<?php

require_once "dbconnect.php";

$city_id = $_POST['city_id'];
if( empty($city_id) ) $city_id = $_GET['city_id'];

//echo "city_id = ".$city_id."<br>";

if( !empty($city_id) ){
	//get sub categories of the city with id = parent_id
	$sql = "SELECT ";
}

$res = $db->query($sql);

//For debug:
//echo "Total categories found: ".$res->num_rows;
//echo "<br>";

if( $res->num_rows == 0 ){
	echo json_encode(array("status"=>"no car found"));
	exit();
}
for( $i=0; $i<$res->num_rows; $i++ ){
	if( !empty($res->rows[$i]['image']) )
		$image = $city_image_root."/".$res->rows[$i]['image'];
	else 
		$image = "";
	$city = array(
		"id"=>$res->rows[$i]['city_id'],
		"name"=>$res->rows[$i]['name'],
		"image"=>$image,
		//"description"=>substr($res->rows[$i]['description'],0, 100),
		//"price"=>$res->rows[$i]['price'],
		"parent_id"=>$res->rows[$i]['parent_id'],
		"total_products"=>get_product_count($res->rows[$i]['city_id'])
	);
	$categories[] = $city;
}
echo json_encode(array("categories"=>$categories));

function get_product_count($city_id){
	global $db;
	$sql = "SELECT * FROM product_to_city WHERE city_id='".$city_id."'";
	$res = $db->query($sql);
	return $res->num_rows;
}

?>
